﻿Imports System.Runtime.CompilerServices
Imports System.Drawing
Imports System.Text.RegularExpressions
Imports System.Windows.Forms

'props to original programmers who came up with some of these awesome code

Public Module ImageMethods

    'original post is from here - http://www.vbforums.com/showpost.php?p=2008553&postcount=2
    <Extension()>
    Public Function Merge(ByVal img1 As Image, ByVal img2 As Image) As Image

        Dim bmp As New Bitmap(Math.Max(img1.Width, img2.Width), img1.Height + img2.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)

        g.DrawImage(img1, 0, 0, img1.Width, img1.Height)
        g.DrawImage(img2, 0, img1.Height, img2.Width, img2.Width)
        g.Dispose()

        Return bmp
    End Function

    'original post is from here - http://www.daniweb.com/software-development/vbnet/threads/151127/712740#post712740
    <Extension()>
    Public Function Fit(ByVal img As Image, ByVal picboxH As Integer, ByVal picboxW As Integer) As Image

        Dim scale_factor As Single

        Dim ImageHeight As Integer
        Dim ImageWidth As Integer

        ImageHeight = img.Height
        ImageWidth = img.Width

        scale_factor = 1.0

        If ImageHeight > picboxH Then
            scale_factor = CSng(picboxH / ImageHeight)
        End If

        If (ImageWidth * scale_factor) > picboxW Then
            scale_factor = CSng(picboxW / ImageWidth)
        End If

        Dim b As New Bitmap(img)
        Dim bmp As New Bitmap(CInt(b.Width * scale_factor), CInt(b.Height * scale_factor))
        Dim g As Graphics = Graphics.FromImage(bmp)

        g.DrawImage(b, 0, 0, bmp.Width + 1, bmp.Height + 1)
        g.Dispose()

        Return bmp
    End Function

End Module

Public Module StringMethods

    'original post is from here - http://www.vbforums.com/showpost.php?p=2483548&postcount=1
    <Extension()>
    Public Function IsEmail(ByVal email As String) As Boolean

        Dim checkPattern As String

        checkPattern = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"

        Dim emailMatch As Match = Regex.Match(email, checkPattern)

        If emailMatch.Success Then
            IsEmail = True
        Else
            IsEmail = False
        End If

    End Function

    'original post is from here - http://www.dotnetperls.com/reverse-string
    <Extension()>
    Public Function ReverseString(ByVal str As String) As String

        Dim c As Char() = str.ToCharArray()
        Array.Reverse(c)

        Return c
    End Function

End Module

Public Module FormMethods

    'original post is from here - http://www.xtremedotnettalk.com/showpost.php?p=398722&postcount=2
    <Extension()>
    Public Function Center(ByVal frm1 As Form, ByVal frm2 As Form) As Point

        Dim x As Integer = (frm1.Width / 2) - (frm2.Width / 2)
        Dim y As Integer = (frm1.Height / 2.2) - (frm2.Height / 2)
        Dim loc As New Point(x, y)

        Return loc
    End Function

End Module